const userModel = require('../models/userModel')
const RoleModel = require('../models/RoleModel')

const userService = {
  register(user) {
    return userModel.create(user)
  },
  login({ username, password }) {
    return userModel.findOne({ username, password })
  },
  getUserInfo(id) {
    return userModel.findById(id).select('-password').populate('role', { _id: 0 })
  },
  updateUserInfo(data) {
    return userModel.updateOne(
      {
        _id: data._id,
      },
      { $set: data }
    )
  },
  getAllUser() {
    return userModel.find().populate('role')
  },
  getAllRole() {
    return RoleModel.find()
  },
  deleteUser(_id) {
    return userModel.deleteOne({
      _id,
    })
  },
}

module.exports = userService
